home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / SCHMOO / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  19KB  |  901 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  *
  4.  * Implementation of the CSchmooDoc derivation of CDocument as
  5.  * well as an implementation of CPolylineAdviseSink.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "schmoo.h"
  18.  
  19.  
  20.  
  21. /*
  22.  * CSchmooDoc::CSchmooDoc
  23.  * CSchmooDoc::~CSchmooDoc
  24.  *
  25.  * Constructor Parameters:
  26.  *  hInst           HINSTANCE of the application.
  27.  */
  28.  
  29. CSchmooDoc::CSchmooDoc(HINSTANCE hInst)
  30.     : CDocument(hInst)
  31.     {
  32.     m_pPL=NULL;
  33.     m_pPLAdv=NULL;
  34.     m_uPrevSize=SIZE_RESTORED;
  35.     return;
  36.     }
  37.  
  38.  
  39. CSchmooDoc::~CSchmooDoc(void)
  40.     {
  41.     //Clean up the allocations we did in FInit
  42.     if (NULL!=m_pPL)
  43.         delete m_pPL;
  44.  
  45.     if (NULL!=m_pPLAdv)
  46.         delete m_pPLAdv;
  47.  
  48.     return;
  49.     }
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. /*
  57.  * CSchmooDoc::FInit
  58.  *
  59.  * Purpose:
  60.  *  Initializes an already created document window.  The client actually
  61.  *  creates the window for us, then passes that here for further
  62.  *  initialization.
  63.  *
  64.  * Parameters:
  65.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  66.  *
  67.  * Return Value:
  68.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  69.  */
  70.  
  71. BOOL CSchmooDoc::FInit(LPDOCUMENTINIT pDI)
  72.     {
  73.     RECT        rc;
  74.  
  75.     //Change the stringtable range to our customization.
  76.     pDI->idsMin=IDS_DOCUMENTMIN;
  77.     pDI->idsMax=IDS_DOCUMENTMAX;
  78.  
  79.     //Do default initialization
  80.     if (!CDocument::FInit(pDI))
  81.         return FALSE;
  82.  
  83.     //Add the Polyline stuff we need.
  84.     m_pPLAdv=new CPolylineAdviseSink((LPVOID)this);
  85.     m_pPL   =new CPolyline(m_hInst);
  86.  
  87.     //Attempt to create our contained Polyline.
  88.     GetClientRect(m_hWnd, &rc);
  89.     InflateRect(&rc, -8, -8);
  90.  
  91.     if (!m_pPL->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE
  92.         , ID_POLYLINE, m_pPLAdv))
  93.         return FALSE;
  94.  
  95.     return TRUE;
  96.     }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. /*
  105.  * CSchmooDoc::FMessageHook
  106.  *
  107.  * Purpose:
  108.  *  Processes WM_SIZE for the document so we can resize the Polyline.
  109.  *
  110.  * Parameters:
  111.  *  <WndProc Parameters>
  112.  *  pLRes           LRESULT FAR * in which to store the return value
  113.  *                  for the message.
  114.  *
  115.  * Return Value:
  116.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  117.  */
  118.  
  119. BOOL CSchmooDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  120.     , LPARAM lParam, LRESULT FAR *pLRes)
  121.     {
  122.     UINT        dx, dy;
  123.     RECT        rc;
  124.  
  125.     if (WM_SIZE==iMsg)
  126.         {
  127.         //Don't effect the Polyline size to or from minimized state.
  128.         if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
  129.             {
  130.             //When we change size, resize any Polyline we hold.
  131.             dx=LOWORD(lParam);
  132.             dy=HIWORD(lParam);
  133.  
  134.             /*
  135.              * If we are getting WM_SIZE in response to a Polyline
  136.              * notification, then don't resize the Polyline window again.
  137.              */
  138.             if (!m_fNoSize && NULL!=m_pPL)
  139.                 {
  140.                 //Resize the polyline to fit the new client
  141.                 SetRect(&rc, 8, 8, dx-8, dy-8);
  142.                 m_pPL->RectSet(&rc, FALSE);
  143.  
  144.                 /*
  145.                  * We consider sizing something that makes the file dirty,
  146.                  * but not until we've finished the create process, which
  147.                  * is why we set fNoDirty to FALSE in WM_CREATE since we
  148.                  * get a WM_SIZE on the first creation.
  149.                  */
  150.                 if (!m_fNoDirty)
  151.                     FDirtySet(TRUE);
  152.  
  153.                 SetRect(&rc, 0, 0, dx, dy);
  154.  
  155.                 if (NULL!=m_pAdv)
  156.                     m_pAdv->OnSizeChange((LPCDocument)this, &rc);
  157.  
  158.                 m_fNoDirty=FALSE;
  159.                 }
  160.             }
  161.  
  162.         m_uPrevSize=wParam;
  163.         }
  164.  
  165.     /*
  166.      * We return FALSE even on WM_SIZE so we can let the default procedure
  167.      * handle maximized MDI child windows appropriately.
  168.      */
  169.     return FALSE;
  170.     }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179. /*
  180.  * CSchmooDoc::Clear
  181.  *
  182.  * Purpose:
  183.  *  Sets all contents in the document back to defaults with no filename.
  184.  *
  185.  * Paramters:
  186.  *  None
  187.  *
  188.  * Return Value:
  189.  *  None
  190.  */
  191.  
  192. void CSchmooDoc::Clear(void)
  193.     {
  194.     //Completely reset the polyline
  195.     m_pPL->New();
  196.  
  197.     CDocument::Clear();
  198.     m_lVer=0;
  199.     return;
  200.     }
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207. /*
  208.  * CSchmooDoc::ULoad
  209.  *
  210.  * Purpose:
  211.  *  Loads a given document without any user interface overwriting the
  212.  *  previous contents of the Polyline window.  We do this by opening
  213.  *  the file and telling the Polyline to load itself from that file.
  214.  *
  215.  * Parameters:
  216.  *  fChangeFile     BOOL indicating if we're to update the window title
  217.  *                  and the filename from using this file.
  218.  *  pszFile         LPSTR to the filename to load, NULL if the file is
  219.  *                  new and untitled.
  220.  *
  221.  * Return Value:
  222.  *  UINT            An error value from DOCERR_*
  223.  */
  224.  
  225. UINT CSchmooDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  226.     {
  227.     //CHAPTER5MOD
  228.     HRESULT         hr;
  229.     LPSTORAGE       pIStorage;
  230.     //End CHAPTER5MOD
  231.  
  232.     if (NULL==pszFile)
  233.         {
  234.         //For a new untitled document, just rename ourselved.
  235.         Rename(NULL);
  236.         m_lVer=VERSIONCURRENT;
  237.         return DOCERR_NONE;
  238.         }
  239.  
  240.     //CHAPTER5MOD
  241.     /*
  242.      * If this is not a Compound File, open the file using STGM_CONVERT
  243.      * in TRANSACTED mode to effectively see old files as a storage with
  244.      * one stream called "CONTENTS" (which is conveniently the name we use
  245.      * in the new files).  We must use STGM_TRANSACTED here or else
  246.      * the old file will be immediately converted on disk:  we only want
  247.      * a converted image in memory from which to read.  In addition,
  248.      * note that we need STGM_READWRITE as well since conversion is
  249.      * inherently a write operation.
  250.      */
  251.  
  252.     pIStorage=NULL;
  253.  
  254.     if (NOERROR!=StgIsStorageFile(pszFile))
  255.         {
  256.         hr=StgCreateDocfile(pszFile, STGM_TRANSACTED | STGM_READWRITE
  257.             | STGM_CONVERT | STGM_SHARE_EXCLUSIVE, 0, &pIStorage);
  258.  
  259.         if (FAILED(hr))
  260.             {
  261.             //If we were denied write access, try to load the old way
  262.             if (STG_E_ACCESSDENIED==GetScode(hr))
  263.                 m_lVer=m_pPL->ReadFromFile(pszFile);
  264.             else
  265.                 return DOCERR_COULDNOTOPEN;
  266.             }
  267.         }
  268.     else
  269.         {
  270.         hr=StgOpenStorage(pszFile, NULL, STGM_DIRECT | STGM_READ
  271.             | STGM_SHARE_EXCLUSIVE, NULL, 0, &pIStorage);
  272.  
  273.         if (FAILED(hr))
  274.             return DOCERR_COULDNOTOPEN;
  275.         }
  276.  
  277.     if (NULL!=pIStorage)
  278.         {
  279.         m_lVer=m_pPL->ReadFromStorage(pIStorage);
  280.         pIStorage->Release();
  281.         }
  282.     //End CHAPTER5MOD
  283.  
  284.     if (POLYLINE_E_READFAILURE==m_lVer)
  285.         return DOCERR_READFAILURE;
  286.  
  287.     if (POLYLINE_E_UNSUPPORTEDVERSION==m_lVer)
  288.         return DOCERR_UNSUPPORTEDVERSION;
  289.  
  290.     if (fChangeFile)
  291.         Rename(pszFile);
  292.  
  293.     //Importing a file makes things dirty
  294.     FDirtySet(!fChangeFile);
  295.  
  296.     return DOCERR_NONE;
  297.     }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305. /*
  306.  * CSchmooDoc::USave
  307.  *
  308.  * Purpose:
  309.  *  Writes the file to a known filename, requiring that the user has
  310.  *  previously used FileOpen or FileSaveAs in order to have a filename.
  311.  *
  312.  * Parameters:
  313.  *  uType           UINT indicating the type of file the user requested
  314.  *                  to save in the File Save As dialog.
  315.  *  pszFile         LPSTR under which to save.  If NULL, use the current name.
  316.  *
  317.  * Return Value:
  318.  *  UINT            An error value from DOCERR_*
  319.  */
  320.  
  321. UINT CSchmooDoc::USave(UINT uType, LPSTR pszFile)
  322.     {
  323.     LONG        lVer, lRet;
  324.     UINT        uTemp;
  325.     BOOL        fRename=TRUE;
  326.     //CHAPTER5MOD
  327.     HRESULT     hr;
  328.     LPSTORAGE   pIStorage;
  329.     //End CHAPTER5MOD
  330.  
  331.     if (NULL==pszFile)
  332.         {
  333.         fRename=FALSE;
  334.         pszFile=m_szFile;
  335.         }
  336.  
  337.     /*
  338.      * Type 1 is the current version, type 2